﻿/******************************************************************************
'Description:报表系统登录类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:zhunan
'create Date:2017.06.22
 ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ReportBusiness
{
    public class uMESLoginBusiness
    {
        public uMESReportOracleDAL dal = new uMESReportOracleDAL();

        public bool Login(string strUid,string password, out string strInfo)
        {
            ADHelper help = new ADHelper();
            string sql = string.Format("select t.password from employee t where UPPER(t.employeename)='{0}' ", strUid.ToUpper());
            DataTable dt = dal.GetDataTable(sql);
            if (dt.Rows.Count == 0)
            {
                strInfo = "登录失败，未找到用户！";
                return false;
            }

            string pwd = dt.Rows[0]["password"].ToString();

            if ((pwd == password) || (help.Encrypto(password) == pwd))
            {
                strInfo = "";
                return true;
            }
            else
            {
                strInfo = "登录失败，密码错误！";
                return false;
            }

        }


        // 登录成功时，根据用户名获取用户相关信息 add zhunan 2021.12.27
        public DataTable getEmployeeInfo(string username)
        {
            string sql = @"
SELECT e.employeename,e.employeeid,e.fullname,e.cardnum,e.password,
       f.factoryname,f.factoryid,
       wc.workcentername,wc.workcenterid,
       o.operationname,
       eroles.rolenames,
       s.shiftname,nvl(e.shiftid,te.shiftid) shiftid,
       te.teamname,te.teamid,
       orgf.factoryname orgfactory,orgwc.workcentername orgworkcenter,
       orgt.teamname orgteam
FROM employee e 
LEFT JOIN sessionvalues sv ON sv.sessionvaluesid = e.sessionvaluesid 
LEFT JOIN factory f ON f.factoryid = sv.factoryid 
LEFT JOIN workcenter wc ON wc.workcenterid = sv.workcenterid 
LEFT JOIN operation o ON o.operationid = sv.operationid
LEFT JOIN 
( 
  SELECT e.employeeid,listagg(r.rolename,'、') WITHIN GROUP (ORDER BY r.rolename) rolenames
  FROM employeerole er 
  LEFT JOIN employee e ON e.employeeid = er.employeeid  
  LEFT JOIN roledef r ON r.roleid = er.roleid  
  GROUP BY e.employeeid 
) eroles ON eroles.employeeid = e.employeeid 
LEFT JOIN 
(
  SELECT  t.teamname,t.teamid,te.employeeid,t.shiftid 
  FROM teamemployee te 
  LEFT JOIN team t ON t.teamid = te.teamid 
) te  ON te.employeeid = e.employeeid AND ROWNUM = 1 
LEFT JOIN factory orgf ON orgf.factoryid = e.orgfactoryid 
LEFT JOIN workcenter orgwc ON orgwc.workcenterid = e.orgworkcenterid 
LEFT JOIN team orgt  ON orgt.teamid = e.orgteamid
LEFT JOIN shift s ON s.shiftid = nvl(e.shiftid,te.shiftid)
WHERE LOWER(e.employeename) = LOWER('{0}')
";
            sql = string.Format(sql, username.ToUpper());
            DataTable dtResult = dal.GetDataTable(sql);
            return dtResult;
        }

        // add by zhunan on 2017-06-22
        // 根据用户ID，查询该用户名下有权限观看的所有报表页面
        public DataTable getPageListByEmployeeId(string employeeId)
        {
            string sql = @"
SELECT DISTINCT rp.pageid reportpageid,rp.pagename reportpagename,
       decode(rp.pagetype,'报表','0','看板','1')  islookboard,
       rp.pageurl,
       rp.factoryid,rp.workcenterid,e.employeeid,
       rp.pagetype,
       decode(rp.isnew,1,'1','0') isnew
FROM employee e 
LEFT JOIN mes_employeereportpage ep ON ep.employeeid = e.employeeid 
LEFT JOIN mes_reportpage rp ON rp.pageid = ep.pageid
WHERE e.employeeid = '{0}' AND rp.status = 1
ORDER BY rp.pagetype,rp.pagename
";
            sql = string.Format(sql, employeeId);
            DataTable dtResult = dal.GetDataTable(sql);
            return dtResult;
        }

        public DataTable GetFactoryNameForLoginName(string strLogName)
        {
            string strSql = "select t.fullname,t.employeeid,w.workcenterid, w.workcentername,t.employeename,r.roleid,r.rolename,f.factoryid,f.factoryname,re.resourceid,re.resourcename from employee t "; //,tt.teamid, tt.teamname
            strSql += "left join employeerole er on er.employeeid=t.employeeid ";
            strSql += "left join roledef r on r.roleid=er.roleid ";
            strSql += "left join sessionvalues se on se.sessionvaluesid=t.sessionvaluesid ";
            strSql += "left join factory f on f.factoryid = se.factoryid ";
            strSql += "left join workcenter w on w.workcenterid = se.workcenterid ";
            strSql += "left join resourcedef  re on re.resourceid=t.resourceid ";
            strSql += " where lower(t.employeename) = '" + strLogName.ToLower() + "'";
            DataTable dtResult = dal.GetDataTable(strSql.ToString());
            return dtResult;
        }
    }
}
